<?php
function GetBetContent3( $bettype, $numbers )
{
    global $g_betname;
    global $g_bignum;
    global $g_chinayear;
    global $g_colorname;
    $name1 = array( "", "单", "双", "大", "小", "红波", "蓝波", "绿波", "合单", "合双" );
    switch ( $bettype )
    {
        case 0 :
            return "特码 ".d2( $numbers );
        case 1 :
            return "特 ".( $numbers == 1 ? "单" : "双" );
        case 2 :
            return "特 ".( $numbers == 1 ? "大" : "小" );
        case 3 :
            return "特 合".( $numbers == 1 ? "单" : "双" );
        case 4 :
            return "正码 ".d2( $numbers );
        case 5 :
            return "总 ".( $numbers == 1 ? "单" : "双" );
        case 6 :
            return "总 ".( $numbers == 1 ? "大" : "小" );
        case 7 :
            break;
        case 8 :
            break;
        case 9 :
            break;
        case 33 :
            break;
        case 10 :
            break;
        case 32 :
            break;
        case 11 :
            break;
        case 29 :
            break;
        case 39 :
            break;
        case 40 :
            break;
        case 41 :
            break;
        case 42 :
            break;
        case 43 :
            break;
        case 44 :
            break;
        case 45 :
    }
    $p = explode( "|", $numbers );
    return $g_betname[$bettype]."@".$p[1]."|".$p[3]."×".$p[2]."组";
    switch ( $bettype )
    {
        case 12 :
            $p = $g_betname[$bettype]." ";
            $p1 = explode( ",", $numbers );
            foreach ( $p1 as $p2 )
            {
                $p3 = explode( "_", $p2 );
                $p .= "正码".$g_bignum[$p3[0]]." ".$name1[$p3[1]]."@".$p3[2]."|";
            }
            $p = substr( $p, 0, 0 - 2 );
            return $p;
        case 13 :
            return "特 ".$g_colorname[$numbers]."波";
        case 14 :
            return "特 肖".$g_chinayear[$numbers];
        case 15 :
            $p = explode( " ", $numbers );
            return "正码".$g_bignum[$p[0]].( $p[1] == 1 ? " 单" : " 双" );
        case 16 :
            $p = explode( " ", $numbers );
            return "正码".$g_bignum[$p[0]].( $p[1] == 3 ? " 大" : " 小" );
        case 17 :
            $p = explode( " ", $numbers );
            return "正码".$g_bignum[$p[0]]." ".$name1[$p[1]];
        case 18 :
            $p0 = "";
            $p1 = "";
            switch ( $numbers )
            {
                case 1 :
                    break;
                case 2 :
                    break;
                case 3 :
                    break;
                case 4 :
            }
            $p0 = 1;
            $p1 = $numbers;
            break;
            switch ( $numbers )
            {
                case 5 :
                    break;
                case 6 :
                    break;
                case 7 :
                    break;
                case 8 :
            }
            $p0 = 2;
            $p1 = $numbers - 4;
            break;
            switch ( $numbers )
            {
                case 9 :
                    break;
                case 10 :
                    break;
                case 11 :
                    break;
                case 12 :
            }
            $p0 = 3;
            $p1 = $numbers - 8;
            if ( $p1 < 3 )
            {
                return "半波 ".$g_colorname[$p0].( $p1 == 1 ? "单" : "双" );
            }
            return "半波 ".$g_colorname[$p0].( $p1 == 3 ? "大" : "小" );
        case 19 :
            $p1 = explode( "|", $numbers );
            $p2 = explode( ",", $p1[1] );
            $p3 = explode( "_", $p1[0] );
            $n1 = $p3[0];
            $n2 = intval( $p3[1] );
            $op = $n2."肖 ".( $n1 == 1 ? "中" : "不中" )."|";
            $op .= $g_chinayear[$p2[0]];
            $i = 1;
            while ( $i < $n2 )
            {
                $op .= ",".$g_chinayear[$p2[$i]];
                ++$i;
            }
            return $op;
        case 20 :
            return "特 ".( $numbers == 1 ? "家禽" : "野兽" );
        case 21 :
            return "一肖".$g_chinayear[$numbers];
        case 37 :
            return "一肖".$g_chinayear[$numbers];
        case 22 :
            return "尾数".substr( $numbers, 0 - 1 );
        case 38 :
            return "尾数".substr( $numbers, 0 - 1 );
        case 23 :
            $p = explode( " ", $numbers );
            return "正码 ".$g_bignum[$p[0]].( $p[1] == 8 ? "合单" : "合双" );
        case 24 :
            return "特码 ".d2( $numbers );
        case 25 :
            $p = explode( " ", $numbers );
            return "正特".$g_bignum[$p[0]]." ".d2( $p[1] );
        case 26 :
            $p = explode( " ", $numbers );
            return "正特".$g_bignum[$p[0]]." ".d2( $p[1] );
        case 27 :
            $p1 = explode( "|", $numbers );
            $p2 = explode( ",", $p1[1] );
            $op = $g_bignum[$p1[0]]."肖 ";
            $i = 0;
            while ( $i < $p1[0] )
            {
                if ( 0 < $i )
                {
                    $op .= ",";
                }
                $op .= $g_chinayear[$p2[$i]];
                ++$i;
            }
            return $op;
        case 28 :
            return "特尾 ".( $numbers == 1 ? "大" : "小" );
        case 30 :
            return "合尾 ".( $numbers == 1 ? "大" : "小" );
        case 31 :
            return "正码 ".d2( $numbers );
        case 34 :
            return "总肖 ".( $numbers == 4 ? "234" : $numbers );
        case 35 :
            return "总肖 ".( $numbers == 1 ? "单" : "双" );
        case 36 :
            $pp = explode( "|", $numbers );
            $p1 = intval( substr( $pp[0], 0, 1 ) );
            $p2 = intval( substr( $pp[0], 1, 1 ) );
            $p3 = explode( ",", $pp[1] );
            $op = $g_bignum[$p1]."尾".( $p2 == 1 ? "中" : "不中" );
            $i = 0;
            while ( $i < $p1 )
            {
                if ( 0 < $i )
                {
                    $op .= ",";
                }
                $op .= $p3[$i];
                ++$i;
            }
            return $op;
        case 46 :
            return "特大中小 ".$g_bigmidname[$numbers];
        case 47 :
            return "特 合".( $numbers == 1 ? "大" : "小" );
    }
}

function outputBrowser( )
{
    global $cc;
    global $g_conf;
    $str = ob_get_contents();
    ob_end_clean();
    echo iconv("UTF-8","gbk//IGNORE",$str);
    ob_start();
}

if ( !defined( "IN_OP" ) )
{
    exit( "You can't access this file directly..." );
}
include_once( "class/ccharset.php" );
$cc = new CCharset();
$ui = new UI();
if ( $curruser['prev_id'] == 0 )
{
    include( "admin_menu.php" );
}
else
{
    include( "admin_menu1.php" );
}
$betcount = 0;
$rs = getdb( "select id,round from lotto where open='".date( "Y-m-d" )."' order by open" );
if ( !$rs->eof )
{
    $lotto_id = $rs->f[0];
    $round = $rs->f[1];
    $rs = getdb( "select count(id) from bet where lotto_id=".$lotto_id );
    $betcount = $rs->f[0];
}
if ( $betcount == 0 )
{
    $ui->showErrorPage( "本期没有任何注单！" );
    exit();
}
$rs->close();
$dataname = G_DBDATABASE;
while ( @ob_end_clean( ) )
{
}
@ob_start();
header( "Pragma: no-cache" );
header( "Expires: 0" );
header( "Content-disposition: attachment; filename=".substr( $dataname, 0, 1 )."_".substr( $dataname, 0 - 1 )."-".date( "Ymd" ).".csv" );
header( "Content-type: plain/text" );
$Year = date( "Y" );
$Month = date( "m" );
$Day = date( "d" );
$acl_name = array( "", "会员", "代理", "总代", "股东", "大股" );
$search = array( "\x00", "\n", "\r", "\x1A" );
$replace = array( "\\0", "\\n", "\\r", "\\Z" );
$DataCreation = strftime( "%d %B %Y", mktime( 0, 0, 0, $Month, $Day, $Year ) );
$Time = date( "H:i:s" );
$crlf = "\r\n";
$i = 0;
$schema_insert = "";
$table = "";
$schema_create = "";
$betArr = array( "bet", "bet2" );
foreach ( $betArr as $key => $table )
{
    echo "".$crlf."";
    if ( $table == "bet" )
    {
        echo "会员注单明细表".$crlf."";
        echo "单号,会员帐号,期数,时间,下注内容,金额,赔率,代理降倍,总代降倍,股东降倍,大股东降倍,公司降倍,盘类,代理占成,总代占成,股东占成,大股东占成,公司占成,IP".$crlf."";
        $fields = "B.id,K.account,B.lotto_id,B.createtime,B.bettype,B.line_nums,B.amount,B.rate,B.rate1,B.rate2,B.rate3,B.rate4,B.rate5,B.disk,B.mpercent,B.percent2,B.percent3,B.percent4,B.upercent,B.ip";
    }
    else
    {
        echo "管理走飞明细表".$crlf."";
        echo "单号,管理帐号,期数,时间,阶层,走飞内容,金额,赔率,总代降倍,股东降倍,大股东降倍,公司降倍,总代占成,股东占成,大股东占成,公司占成,模式".$crlf."";
        $fields = "B.id,K.account,B.lotto_id,B.createtime,B.acl,B.bettype,B.line_nums,B.amount,B.rate,B.rate2,B.rate3,B.rate4,B.rate5,B.percent3,B.percent4,B.percent5,B.percent255,B.rame";
    }
    outputBrowser();
    if ( !( $result = mysql_db_query( G_DBDATABASE, "SELECT {$fields} FROM ".$table." B LEFT JOIN k_user K ON K.id = B.user_id WHERE B.lotto_id=".$lotto_id." order by B.id desc " ) ) )
    {
        exit( "Can't select data from {$table}" );
    }
    $a = 0;
    $schema_insert = "";
    while ( $row = mysql_fetch_row( $result ) )
    {
        $schema_insert = "";
        $fqty = mysql_num_fields( $result );
        $j = 0;
        while ( $j < $fqty )
        {
            if ( mysql_field_name( $result, $j ) == "lotto_id" )
            {
                $schema_insert .= " ".$round.",";
            }
            else if ( mysql_field_name( $result, $j ) == "createtime" )
            {
                $schema_insert .= " ".date( "m-d H:i:s", $row[$j] ).",";
            }
            else if ( mysql_field_name( $result, $j ) == "acl" )
            {
                $schema_insert .= " ".$acl_name[$row[$j]].",";
            }
            else if ( mysql_field_name( $result, $j ) == "bettype" )
            {
                $schema_insert .= " ".GetBetContent3( $row[$j], $row[$j + 1] ).",";
            }
            else if ( mysql_field_name( $result, $j ) == "line_nums" )
            {
                $schema_insert .= "";
            }
            else if ( mysql_field_name( $result, $j ) == "disk" )
            {
                $schema_insert .= " ".$g_disk_code[$row[$j]].",";
            }
            else if ( !isset( $row[$j] ) )
            {
                $schema_insert .= " NULL,";
            }
            else if ( $row[$j] != "" )
            {
                $schema_insert .= " ".str_replace( $search, $replace, addslashes( $row[$j] ) ).",";
            }
            else
            {
                $schema_insert .= " ,";
            }
            ++$j;
        }
        $schema_insert = ereg_replace( ",\$", "", $schema_insert );
        $schema_insert .= "".$crlf;
        echo $schema_insert;
        ++$a;
        outputBrowser();
    }
    ++$i;
}
echo "-----------------------------------------------END-----------------------------------------------------".$crlf."";
?>